//-----------------Sonic CD LaserTrap Script-------------------//
// Yeah...

// Aliases
#alias Object.Value1			:	Object.Timer
#alias Object.Value2			:	Object.YDrawPos
#alias Object.Value3			:	Object.T1
#alias Object.Value4			:	Object.T2
#alias Object.Value5			:	Object.T3
#alias Object.Value5			:	Object.T4
#alias Object.Value6			:	Object.T5
#alias Object.PropertyValue		:	PushButton.Pressed	

// HUD alias
#alias Object[24].PropertyValue	:	HUD.CurrentTimePeriod

// States
#alias 0	:	LASER_ON
#alias 1	:	LASER_OFF

// Priority
#alias 0	:	PRIORITY_BOUNDS
#alias 1	:	PRIORITY_ACTIVE

// Time Periods
#alias 1	:	TIME_PAST

sub ObjectMain
	if Object.OutOfBounds == true
		Object.State    = LASER_ON
		Object.Priority = PRIORITY_BOUNDS
	end if

	if PushButton[-1].Pressed == true
		Object.State    = LASER_OFF
	end if
end sub

sub ObjectPlayerInteraction
	if Object.State == LASER_ON
		TempValue0 = -16
		TempValue0 *= Object.PropertyValue
		TempValue0 += -16

		PlayerObjectCollision(C_TOUCH, -9, TempValue0, 9, 0)
		if CheckResult == true
			CallFunction(Player_Hit)
		end if
	end if
end sub


sub ObjectDraw
	if HUD.CurrentTimePeriod == TIME_PAST
		Object.Frame = 0
		Object.Timer = 0
		Object.State = LASER_OFF
	else
		if Object.State == LASER_ON
			Object.Timer += 8
		end if

		if Object.Timer > 678
			Object.Timer = 0
		end if
			
		if  Object.Timer <= 8
			if Object.OutOfBounds == false
				PlayStageSFX(7, false)
			end if
		end if

		switch Object.Timer
		case 0
			Object.State = LASER_ON
			Object.Frame = 1
			break
		
		case 24
			Object.Frame = 0
			break

		case 48
			Object.Frame = 1
			break

		case 64
			Object.Frame = 0
			break

		case 104
			Object.Frame = 4
			break

		case 128
			Object.Frame = 0
			break

		case 144
			Object.Frame = 1
			break

		case 168
			Object.Frame = 0
			break
		
		case 272
			Object.Frame = 2
			break
		
		case 312
			Object.Frame = 4
			break
		
		case 336
			Object.Frame = 0
			break

		case 352
			Object.Frame = 3
			break

		case 376
			Object.Frame = 0
			break
		end switch

		if  Object.Timer > 410
			Object.Timer += 2
			Object.State = LASER_OFF
		end if
	end if
	
	TempValue0 = Object.Frame
	TempValue0 *= 3
	DrawSprite(TempValue0)

	TempValue0 = 0
	TempValue1 = 16

	TempValue3 = Object.Frame
	TempValue3 *= 3
	TempValue3 += 1
	while TempValue0 < Object.PropertyValue
		TempValue2  = -512
		TempValue2  *= TempValue1
		TempValue2 <<= 7
		TempValue2  += Object.YPos

		DrawSpriteXY(TempValue3, Object.XPos, TempValue2)

		TempValue0++

		TempValue1 += 16
	loop

	Object.YDrawPos = -512
	Object.YDrawPos  *= TempValue1
	Object.YDrawPos <<= 7
	Object.YDrawPos  += Object.YPos
	Object.YDrawPos  &= 0xFFFF0000 // Truncate the value

	TempValue0 = Object.Frame
	TempValue0 *= 3
	TempValue0 += 2
	DrawSpriteXY(TempValue0, Object.XPos, Object.YDrawPos)
end sub


sub ObjectStartup
	LoadSpriteSheet("R2/Objects3.gif")
	
	// Set this object to always active
	ArrayPos0 = 32
	while ArrayPos0 < 1056
		if Object[ArrayPos0].Type == TypeName[LaserTrap]
			Object[ArrayPos0].Priority = PRIORITY_ACTIVE
		end if
		ArrayPos0++
	loop
	
	TempValue0 = HUD.CurrentTimePeriod
	TempValue1 = 1
	TempValue0 *= 17
	TempValue1 += TempValue0 
	
	SpriteFrame(-8, -8, 16, 16, TempValue1, 18)			// #0 - Bottom
	SpriteFrame(-8, -8, 16, 16, 1, 171)					// #1 - Laser 
	SpriteFrame(-8, -8, 16, 16, TempValue1, 1)			// #2 - Top
	
	SpriteFrame(-8, -8, 16, 16, TempValue1, 52)			// #3 - Bottom
	SpriteFrame(-8, -8, 16, 16, 1, 188)					// #4 - Laser 
	SpriteFrame(-8, -8, 16, 16, TempValue1, 35)			// #5 - Top
	
	SpriteFrame(-8, -8, 16, 16, TempValue1, 86)			// #6 - Bottom
	SpriteFrame(-8, -8, 16, 16, 1, 205)					// #7 - Laser 
	SpriteFrame(-8, -8, 16, 16, TempValue1, 69)			// #8 - Top
	
	SpriteFrame(-8, -8, 16, 16, TempValue1, 120)		// #9 - Bottom
	SpriteFrame(-8, -8, 16, 16, 1, 222)					// #10 - Laser 
	SpriteFrame(-8, -8, 16, 16, TempValue1, 103)		// #11 - Top
	
	SpriteFrame(-8, -8, 16, 16, TempValue1, 154)		// #12 - Bottom
	SpriteFrame(-8, -8, 16, 16, 1, 239)					// #13 - Laser 
	SpriteFrame(-8, -8, 16, 16, TempValue1, 137)		// #14 - Top
end sub 

// ========================
// Editor Subs
// ========================

sub RSDKDraw
	DrawSprite(0)
	
	TempValue0 = 0
	TempValue1 = 16
	while TempValue0 < Object.PropertyValue
		TempValue3  = -512
		TempValue3  *= TempValue1
		TempValue3 <<= 7
		TempValue3  += Object.YPos

		DrawSpriteXY(1, Object.XPos, TempValue3)

		TempValue0++

		TempValue1 += 16
	loop

	Object.YDrawPos = -512
	Object.YDrawPos  *= TempValue1
	Object.YDrawPos <<= 7
	Object.YDrawPos  += Object.YPos
	Object.YDrawPos  &= 0xFFFF0000 // Truncate the value

	DrawSpriteXY(2, Object.XPos, Object.YDrawPos)
	
	// Draw an line instead of the laser, it is easier
	TempValue0 = object.xpos
	TempValue0 -= 0x1 0000
	DrawLine(TempValue0, object.ypos, TempValue0, Object.YDrawPos, 0xE0, 0xE0, 0)
	TempValue0 += 0x1 0000
	DrawLine(TempValue0, object.ypos, TempValue0, Object.YDrawPos, 0xE0, 0xE0, 0)
	TempValue0 += 0x1 0000
	DrawLine(TempValue0, object.ypos, TempValue0, Object.YDrawPos, 0xE0, 0xE0, 0)
		
end sub


sub RSDKLoad
	LoadSpriteSheet("R2/Objects3.gif")
	SpriteFrame(-8, -8, 16, 16, 1, 18)			// #0 - Bottom
	SpriteFrame(-8, -8, 16, 16, 1, 171)		// #1 - Laser 
	SpriteFrame(-8, -8, 16, 16, 1, 1)			// #2 - Top
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "height")
end sub